package com.jinunn.hosp.controller.api;

import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.model.hosp.Schedule;
import com.atguigu.yygh.vo.hosp.DepartmentQueryVo;
import com.atguigu.yygh.vo.hosp.ScheduleQueryVo;
import com.jinunn.common.exception.YyghException;
import com.jinunn.common.result.Result;
import com.jinunn.common.result.ResultCodeEnum;
import com.jinunn.common.utils.MD5;
import com.jinunn.hosp.service.DepartmentService;
import com.jinunn.hosp.service.HospitalService;
import com.jinunn.hosp.service.HospitalSetService;
import com.jinunn.hosp.service.ScheduleService;
import com.jinunn.yygh.helper.HttpRequestHelper;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author : JinDun
 * @date : 2021/4/27 23:48
 */
@RestController
@RequestMapping("/api/hosp")
public class ApiController {

    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private HospitalSetService hospitalSetService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private ScheduleService scheduleService;

    @ApiOperation("上传医院接口")
    @PostMapping("saveHospital")
    public Result<Hospital> saveHospital(HttpServletRequest request){
        Map<String, Object> switchMap = getSwitchMap(request);

        String logoData = (String) switchMap.get("logoData");
        String all = logoData.replaceAll(" ", "+");
        switchMap.put("logoData",all);

        hospitalService.save(switchMap);
        return Result.ok();
    }


    @ApiOperation("根据医院编号查询医院信息")
    @PostMapping("hospital/show")
    public Result<Hospital> getHospital(HttpServletRequest request){
        Map<String, Object> switchMap = getSwitchMap(request);
        String hoscode = (String) switchMap.get("hoscode");
        Hospital hospital =hospitalService.getByHoscode(hoscode);
        return Result.ok(hospital);
    }

    @ApiOperation("添加科室")
    @PostMapping("saveDepartment")
    public Result<Hospital> saveDepartment(HttpServletRequest request){
        Map<String, Object> switchMap = getSwitchMap(request);
        departmentService.save(switchMap);
        return Result.ok();
    }

    @ApiOperation("查询科室接口")
    @PostMapping("department/list")
    public Result<Page<Department>> findDepartment(HttpServletRequest request){
        Map<String, Object> switchMap = getSwitchMap(request);
        //医院编号
        String hoscode = (String)switchMap.get("hoscode");
        //当前页 和 每页记录数
        int page = StringUtils.isEmpty(switchMap.get("page")) ? 1 : Integer.parseInt((String)switchMap.get("page"));
        int limit = StringUtils.isEmpty(switchMap.get("limit")) ? 1 : Integer.parseInt((String)switchMap.get("limit"));

        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);
        //调用service方法
        Page<Department> pageModel = departmentService.findPageDepartment(page,limit,departmentQueryVo);
        return Result.ok(pageModel);
    }


    @ApiOperation("删除科室接口")
    @PostMapping("department/remove")
    public Result<Department> removeDepartment(HttpServletRequest request){
        Map<String, Object> switchMap = getSwitchMap(request);
        String hoscode = (String)switchMap.get("hoscode");
        String depcode = (String)switchMap.get("depcode");
        departmentService.removeDepartment(hoscode,depcode);
        return Result.ok();

    }

    @ApiOperation("添加排版接口")
    @PostMapping("saveSchedule")
    public Result<Schedule> saveSchedule(HttpServletRequest request){
        Map<String, Object> switchMap = getSwitchMap(request);
        scheduleService.save(switchMap);
        return Result.ok();
    }

    @ApiOperation("查询排版接口")
    @PostMapping("schedule/list")
    public Result<Page<Schedule>> findSchedule(HttpServletRequest request){
        Map<String, Object> switchMap = getSwitchMap(request);
        //医院编号
        String hoscode = (String)switchMap.get("hoscode");
        //科室编号
        String depcode = (String)switchMap.get("depcode");
        //当前页 和 每页记录数
        int page = StringUtils.isEmpty(switchMap.get("page")) ? 1 : Integer.parseInt((String)switchMap.get("page"));
        int limit = StringUtils.isEmpty(switchMap.get("limit")) ? 1 : Integer.parseInt((String)switchMap.get("limit"));

        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        scheduleQueryVo.setHoscode(depcode);
        //调用service方法
        Page<Schedule> pageModel = scheduleService.findPageSchedule(page,limit,scheduleQueryVo);
        return Result.ok(pageModel);
    }

    @ApiOperation("删除排班")
    @PostMapping("schedule/remove")
    public Result<Schedule> remove(HttpServletRequest request) {
        Map<String, Object> switchMap = getSwitchMap(request);
        String hoscode = (String)switchMap.get("hoscode");
        String hosScheduleId = (String)switchMap.get("hosScheduleId");
        scheduleService.remove(hosScheduleId,hoscode);
        return Result.ok();
    }

    private Map<String, Object> getSwitchMap(HttpServletRequest request) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> switchMap = HttpRequestHelper.switchMap(parameterMap);

        //获取医院系统中传递过来的签名，签名进行MD5加密
        String hospSign = (String) switchMap.get("sign");

        //根据传递过来的医院编码，查询数据库，查询签名
        String hoscode = (String) switchMap.get("hoscode");
        String singKey = hospitalSetService.getSingKey(hoscode);

        //判断签名是否一致
        if (!hospSign.equals(MD5.encrypt(singKey))){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        return switchMap;
    }
}
